//通用函数
var empty = function( ){

};
var loop = function( total, single ){
	var i;
	for(i = 0; i<total; i++){
		single( i );
	}
};
var foreach = function( li, single ){
	var p;
	for(p in li){
		single( li[p] );
	}
};

// 控制码
var VKEY_W = 87;
var VKEY_S = 83;
var VKEY_A = 65;
var VKEY_D = 68;
var VKEY_SPACE = 32;
var VKEY_WAVE = 192;

//按键间隔
var PRESS_INTERVAL = 50;

//库函数
var Box = null;
var Label = null;
var Timer = null;

//加速移动的轨迹
var accelerationTrace = function( position, velocity, acceleration, time ){
	var rst = [ position ];
	loop( time, function( t ){
		var v = velocity+acceleration*t;
		rst.push( rst[t]+v );
	} );
	return rst;
};
//由始点向终点的减速运动
var accelerationPath = function( from, to, time ){
	//s=(1/2)*a*t^2
	var path = accelerationTrace( to, 0, 2*( from-to )/( time*time ), time );
	//去掉始点
	path.pop( );
	//整数化
	for(p in path){
		path[p] = Math.floor( path[p] );
	}
	//终点置于最后
	return path.reverse( );
};

var parabola = function( from, to, time, gravity ){
	var horizonX = accelerationTrace( to.x, ( from.x-to.x )/time, 0, time );
	horizonX.pop( );
	horizonX.reverse( );
	var horizonY = accelerationTrace( to.y, ( from.y-to.y )/time, 0, time );
	horizonY.pop( );
	horizonY.reverse( );
	var up = accelerationTrace( 0, 0, gravity, time/2 );
	var height = -up.pop( );
	loop( up.length, function( i ){
		up[i] = up[i]+height;
	} );
	up.reverse( );
	var down = accelerationTrace( height, 0, gravity, time/2 );
	if(time%2==0){
		up.pop( );
	}
	var vertical = up.concat( down );
	var rst = [ ];
	loop( time, function( t ){
		rst.push( {
			x: Math.floor( horizonX[t] ),
			y: Math.floor( horizonY[t]+vertical[t] )
		} );
	} );
	return rst;
};

//播放列表
var Playlist = function(  ){
	var cut = Z.Event( );
	var total = 0;
	return {
		empty: function( ){
			return total==0;
		},
		skip: cut.trig,
		play: function( interval, ongoing, after ){
			//初始化
			var count = 0;
			var done = Z.Event( );
			//设置计时器
			var timer = Timer( function( ){
				if(ongoing( count )){
					count++;
				}
				else{
					done.trig( );
					after( );
				}
			}, interval );
			done.regist( timer.stop );
			//强制停止事件
			var onCut = cut.regist( function( ){
				//一次执行完毕
				while(ongoing( count )){
					count++;
				}
				;
				done.trig( );
				after( );
			} );
			done.regist( onCut.remove );
			//列表计数
			total = total+1;
			done.regist( function( ){
				total = total-1;
			} );
			//直接启动计时器
			timer.start( );
		}
	};
};

//移动
var Motion = function( setter, positions ){
	return function( count ){
		if(count<positions.length){
			setter( positions[count] );
			return true;
		}
		else{
			return false;
		}
	};
};

//打字
var Typer = function( label, txt ){
	return function( count ){
		if(count<txt.length){
			label.text( txt.substring( 0, count+1 ) );
			return true;
		}
		else{
			return false;
		}
	};
};

//显隐动画
var Anime = function( objects, interval ){
	//当前帧
	var present = null;
	//计数
	var count = 0;
	//回调
	var cb = empty;
	//计时播放
	var timer = Timer( function( ){
		if(count<objects.length){
			objects[count].visible( true );
			present.visible( false );
			present = objects[count];
			count++;
		}
		else{
			cb( );
		}
	}, interval );

	return {
		play: function( ){
			objects[0].visible( true );
			present = objects[0];
			count = 1;
			timer.start( );
		},
		remove: function( ){
			timer.stop( );
			if(present){
				present.visible( false );
				present = null;
			}
		},
		//reset与先remove再play的执行顺序不同
		reset: function( ){
			objects[0].visible( true );
			present.visible( false );
			present = objects[0];
			count = 1;
		},
		callback: function( callback ){
			cb = callback;
		}
	};
};

//循环播放
var CycleAnime = function( objects, interval ){
	var anime = Anime( objects, interval );
	anime.callback( anime.reset );
	return anime;
};
